﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject;
using Jalal.Library.DataStructure;
using System.Collections.Generic;

public partial class Admin_EmailTemplates_Default : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitControls();
            BindGrid();
        }
    }

    private void InitControls()
    {
        btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete these items?')");
    }

    private void BindGrid()
    {
        ReadWriteCustomCollection<EmailTemplate> templates = EmailTemplate.GetTemplateList();
        grvEmailTemplates.DataSource = templates;
        grvEmailTemplates.DataBind();
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox cbxSelectAll = (CheckBox)e.Row.FindControl("cbxSelectAll");
            cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + grvEmailTemplates.ClientID + "','" + cbxSelectAll.ClientID + "','cbxSelect')");
        }
        else if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal ||
            e.Row.RowState == DataControlRowState.Alternate))
        {
            HyperLink hlkEdit = (HyperLink)e.Row.FindControl("hlkEdit");
            EmailTemplate template = (EmailTemplate)e.Row.DataItem;

            if (hlkEdit != null && template!=null)
            {
                hlkEdit.NavigateUrl = CustomSiteMap.EditNews + "?id=" + template.Id;
            }
        }
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
        List<int> templatesToDelete = new List<int>();

        foreach (GridViewRow row in grvEmailTemplates.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cbxSelect = (CheckBox)row.FindControl("cbxSelect");
                if (cbxSelect != null && cbxSelect.Checked)
                {
                    Label lblId = (Label)row.FindControl("lblId");
                    templatesToDelete.Add(Convert.ToInt32(lblId.Text));
                }
            }
        }
        EmailTemplate.Delete(templatesToDelete);
        BindGrid();
    }
}
